package com.wangwen.wechatmp.utils;

import java.security.MessageDigest;

/**
 * @Author: wangwei
 * @Date: 2019/11/30 12:52
 */

public class MD5Util {
    public static void main(String[] args) {
//        String s = "appid=wx56c01c125d7280ef&body=xface&face_code=Qpoqwhsdjhfausrhqieofnq90w=w8233wdwjdjiwq&mch_id=1498784062&nonce_str=wzuvjoghua&openid=oyTWy0w93YBSoTVs8rFzsjrFBfYc&out_trade_no=2019120211015021055383414&spbill_create_ip=127.0.0.1&total_fee=1&key=3202007009200246561ms05848941904";

       String result =  MD5Encode("151027829wa@qq!@","UTF-8").toUpperCase();
        System.out.println(result);

       String res =  convertMD5(convertMD5("e10adc3949ba59abbe56e057f20f883e"));
        System.out.println(res);
       String s =  JM("e10adc3949ba59abbe56e057f20f883e");
        System.out.println(s);

    }
    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString;
    }

    public static String convertMD5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }
    //解密
    public static String JM(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String k = new String(a);
        return k;
    }



    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}
